package com.beiming.libra.controller;

import com.beiming.libra.common.constants.ValidateMessage;
import com.beiming.libra.domain.dto.responsedto.AreasDTO;
import com.beiming.libra.helper.Result;
import com.beiming.libra.service.AreasService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Rimon Date : 2018/6/7 10:43
 */

@RestController
@Validated
@Slf4j
@Api(value = "地址查询接口", tags = "地址查询接口")
@RequestMapping(value = "api/areas")
public class AreasController {

  @Resource
  private AreasService areasService;

  /**
   * 根据当前地址编码查询下级地址
   *
   * @param code 地址编码
   */
  @PostMapping("/children/{code}")
  @ApiOperation(value = "查询当前地址编码查询下级地址", notes = "查询当前地址编码查询下级地址")
  public Result<List<AreasDTO>> getChildrenAreas(@ApiParam("地址编码") @Valid @NotEmpty(
      message = ValidateMessage.CODE_NOT_BLANK) @PathVariable("code") String code) {
    return Result.success(areasService.getChildrenAreasByCode(code));
  }

  /**
   * 查询省级列表
   */
  @PostMapping("/provinces")
  @ApiOperation(value = "查询省级列表", notes = "查询省级列表")
  public Result<List<AreasDTO>> getProvinces() {
    return Result.success(areasService.getAllProvinces());
  }

  /**
   * 根据当前地址编码查询该省下所有地址
   *
   * @param code 地址编码
   */
  @PostMapping("/allAddress/{code}")
  @ApiOperation(value = "查询当前地址编码所属省份下所有地址(市、区县、街道)", notes = "查询当前地址编码所属省份下所有地址(市、区县、街道)")
  public Result getRelationAreasByCode(
      @ApiParam("地址编码") @Valid @NotEmpty(
          message = ValidateMessage.CODE_NOT_BLANK) @PathVariable("code") String code) {
    return Result.success(areasService.getRelationAreasByCode(code));
  }

}